package com.zt.java.concurrency.lock;

import java.util.concurrent.locks.ReentrantLock;

/**
 * @ClassName: Lock
 * @Description: Lock
 * @Author: tao.zhang1
 * @Date: 2019/9/4 16:25
 * @Version: V1.0
 **/
public class Lock {

    public static void main(String[] args) throws  Exception {
        ReentrantLock lock = new ReentrantLock();
        Thread threadA = new MyThread(lock);
        Thread threadB = new MyThread(lock);
        threadA.start();
        Thread.sleep(2000);
        threadB.start();
    }
}
class MyThread extends Thread{
    ReentrantLock lock ;
    MyThread(ReentrantLock lock){
        this.lock = lock;
    }
    @Override
    public void run(){
        lock.lock();
        while(true){
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
               // lock.unlock();
            }
            System.out.println(this.getName() + "run");
        }
    }
}
